package com.tulele.vtool.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tulele.vtool.mapper.UserCommodityInfoMapper;
import com.tulele.vtool.model.UserCommodityInfo;
import com.tulele.vtool.service.IUserCommodityInfoService;
import com.tulele.vtool.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Service
public class UserCommodityInfoServiceImpl implements IUserCommodityInfoService {

    @Autowired
    private UserCommodityInfoMapper userCommodityInfoMapper;

    @Override
    public UserCommodityInfo getCommodityDetailInfo(Integer commodityId) {
        return userCommodityInfoMapper.queryCommodityDetailInfo(commodityId);
    }

    @Override
    public List<UserCommodityInfo> getUserCommodityInfoBySpecialSize(Integer userId, Integer brandId, int recordSize) {
        return userCommodityInfoMapper.queryUserCommodityInfoBySpecialSize(userId, brandId, recordSize);
    }

    @Override
    public PageInfo<UserCommodityInfo> getUserCommodityInfo(Integer userId, Integer brandId, int pageNum, int PageSize) {

        PageHelper.startPage(pageNum, PageSize);
        List<UserCommodityInfo> userCommodityInfoList = userCommodityInfoMapper.queryUserCommodityInfo(userId, brandId);

        return new PageInfo<UserCommodityInfo>(userCommodityInfoList);
    }

    @Override
    public int pseudoDeleteCommodityInfo(Integer commodityId) {
        UserCommodityInfo userCommodityInfo = new UserCommodityInfo();
        userCommodityInfo.setId(commodityId);
        userCommodityInfo.setCommodityStatus(0);
        userCommodityInfo.setModifyTime(DateUtil.getCurDate());

        userCommodityInfoMapper.updateCommodityInfo(userCommodityInfo);

        return 0;
    }

    @Override
    public int createUserCommodityInfo(UserCommodityInfo userCommodityInfo) {
        return userCommodityInfoMapper.addCommodityInfo(userCommodityInfo);
    }

    @Override
    public int modifyCommodityInfo(UserCommodityInfo userCommodityInfo) {
        return userCommodityInfoMapper.updateCommodityInfo(userCommodityInfo);
    }

    @Override
    public int stickCommodityInfo(Integer userId, Integer commodityId) {

        //查询当前已经置顶的商品信息
        List<UserCommodityInfo> stickCommdityInfoList = userCommodityInfoMapper.queryUserStickCommodityInfo(userId);

        //如果置顶商品信息小于等于一个，则直接置顶，如果大于等于2个，则把置顶最久的顶替掉
        if (CollectionUtils.isEmpty(stickCommdityInfoList) || stickCommdityInfoList.size() <= 1) {

            UserCommodityInfo userCommodityInfo = new UserCommodityInfo();
            userCommodityInfo.setId(commodityId);
            userCommodityInfo.setCommodityStickStatus(1);
            userCommodityInfo.setModifyTime(DateUtil.getCurDate());

            userCommodityInfoMapper.updateCommodityInfo(userCommodityInfo);

            return 0;

        } else {

            int size = stickCommdityInfoList.size();
            UserCommodityInfo oldestCommodityInfo = stickCommdityInfoList.get(size - 1);
            if (oldestCommodityInfo != null) {

                UserCommodityInfo userCommodityInfo = new UserCommodityInfo();
                userCommodityInfo.setId(oldestCommodityInfo.getId());
                userCommodityInfo.setCommodityStickStatus(0);
                userCommodityInfo.setModifyTime(DateUtil.getCurDate());

                userCommodityInfoMapper.updateCommodityInfo(userCommodityInfo);
            }

            UserCommodityInfo userCommodityInfo = new UserCommodityInfo();
            userCommodityInfo.setId(commodityId);
            userCommodityInfo.setCommodityStickStatus(1);
            userCommodityInfo.setModifyTime(DateUtil.getCurDate());

            userCommodityInfoMapper.updateCommodityInfo(userCommodityInfo);

        }


        return 0;
    }

    @Override
    public int removeAllUserCommodityInfo(Integer userId, Integer commodityStatus) {
        return userCommodityInfoMapper.updUserAllCommodityInfo(userId, commodityStatus);
    }

    @Override
    public int getUserBrandCommodityCount(Integer userId, Integer brandId) {
        return userCommodityInfoMapper.queryUserBrandCommodityCount(userId, brandId);
    }

    @Override
    public int addUserPlatformCommodityInfo(Integer userId, Integer brandId) {
        return userCommodityInfoMapper.addUserPlatformCommodityInfo(userId, brandId);
    }
}
